package com.kawa.kemanage.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KAIyo
 * @Date: 2025/6/2 23:15
 * @Version: v1.0.0
 * @Description: TODO
 **/

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();

        // 配置序列化特性
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,        // 输出null值
                SerializerFeature.WriteNullListAsEmpty,     // List为null时输出[]
                SerializerFeature.WriteNullStringAsEmpty,   // String为null时输出""
                SerializerFeature.WriteNullNumberAsZero,    // Number为null时输出0
                SerializerFeature.WriteDateUseDateFormat,   // 格式化日期
                SerializerFeature.DisableCircularReferenceDetect // 禁用循环引用检测
        );

        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);

        // 设置支持的MediaType
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(mediaTypes);

        converters.add(0, converter);
    }
}
